package com.ev.apis.controller.scm;

import com.alibaba.fastjson.JSONArray;
import com.ev.framework.annotation.EvApiByToken;
import com.ev.framework.config.Constant;
import com.ev.framework.utils.EasyPoiUtils;
import com.ev.framework.utils.JSONUtils;
import com.ev.framework.utils.R;
import com.ev.scm.domain.DeliveryNoteDO;
import com.ev.scm.service.IDeliveryNoteService;
import com.google.common.collect.Maps;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


/**
   * Description 发货通知单Api
   * Company Xin Soft
   * @author π
   * Date 2020/12/29
  **/
@RestController
@Api(value = "/",tags = "发货通知单Api")
public class DeliveryNoteApiController {

     /** 发货通知服务 */
    @Resource
    private IDeliveryNoteService iDeliveryNoteService;

    /** slf4j日志 */
    private static final Logger LOGGER = LoggerFactory.getLogger(DeliveryNoteApiController.class);

//    @EvApiByToken(value = "/apis/deliveryNote/saveHandle", method = RequestMethod.POST, apiTitle = "增加或修改发货通知")
//    @ApiOperation("增加或修改发货通知")
//    @Transactional(rollbackFor = Exception.class)
//    public R saveHandle(DeliveryNoteDO deliveryNoteDO,
//                        @ApiParam(value = "发货通知明细:" +
//                           "[\n" +
//                           "    {\n" +
//                           "        \"stockId\":\"28,30\",\n" +
//                           "        \"materielId\":25,\n" +
//                           "        \"batch\":\"wh_ycl_001\",\n" +
//                           "        \"deliveryCount\":201,\n" +
//                           "        \"supportUom\":10012,\n" +
//                           "        \"convCoefficient\":1.000,\n" +
//                           "        \"deliveryCountSupport\":20,\n" +
//                           "        \"sellUnitPrice\":1200,\n" +
//                           "        \"sellAmount\":24000,\n" +
//                           "        \"sourceId\":11,\n" +
//                           "        \"sourceType\":11,\n" +
//                           "        \"sourceCode\":\"LYDH20190720001\"\n" +
//                           "    },\n" +
//                           "]"
//                           , required = true) @RequestParam(value = "bodyItem", required = true)String bodyItem,
//                        @ApiParam(value = "上传附件") @RequestParam(value = "uploadAttachment", defaultValue = "", required = false) String uploadAttachment,
//                        @ApiParam(value = "被删除的销售合同明细id集合") @RequestParam(value = "itemIds", defaultValue = "", required = false) Long[] itemIds) throws Exception {
//        try {
//            return iDeliveryNoteService.saveHandle(deliveryNoteDO,bodyItem,uploadAttachment,itemIds);
//        }catch (Exception e) {
//            e.printStackTrace();
//            LOGGER.error(e.getMessage(),e);
//            throw e;
//        }
//    }

//     @EvApiByToken(value = "/apis/deliveryNote/audit", method = RequestMethod.POST, apiTitle = "审核发货通知")
//     @ApiOperation("审核发货通知")
//     @Transactional(rollbackFor = Exception.class)
//     public R audit(
//             @ApiParam(value = "发货通知主表id", required = true) @RequestParam(value = "id", defaultValue = "") Long id
//     ) throws Exception {
//         try {
//             return iDeliveryNoteService.auditOrReverseAudit(id, Constant.OK_AUDITED);
//         }catch (Exception e) {
//             e.printStackTrace();
//             LOGGER.error(e.getMessage(),e);
//             throw e;
//         }
//     }
//
//     @EvApiByToken(value = "/apis/deliveryNote/reverseAudit", method = RequestMethod.POST, apiTitle = "反审核发货通知")
//     @ApiOperation("反审核发货通知")
//     @Transactional(rollbackFor = Exception.class)
//     public R reverseAudit(
//             @ApiParam(value = "发货通知主表id", required = true) @RequestParam(value = "id") Long id
//     ) throws Exception {
//         try {
//             return iDeliveryNoteService.auditOrReverseAudit(id, Constant.WAIT_AUDIT);
//         }catch (Exception e) {
//             e.printStackTrace();
//             LOGGER.error(e.getMessage(),e);
//             throw e;
//         }
//     }

     @EvApiByToken(value = "/apis/deliveryNote/findList", method = RequestMethod.POST, apiTitle = "发货通知列表查询")
     @ApiOperation("发货通知列表查询")
     public R findList(
             @ApiParam(value = "当前第几页", required = true) @RequestParam(value = "page", defaultValue = "1") int page,
             @ApiParam(value = "一页多少条", required = true) @RequestParam(value = "pageSize", defaultValue = "20") int pageSize,
             @ApiParam(value = "通知单号") @RequestParam(value = "deliveryCode", defaultValue = "", required = false) String deliveryCode,
             @ApiParam(value = "客户名称") @RequestParam(value = "clientName", defaultValue = "", required = false) String clientName,
             @ApiParam(value = "物料名称") @RequestParam(value = "materielName", defaultValue = "", required = false) String materielName,
             @ApiParam(value = "通知开始时间") @RequestParam(value = "startTime", defaultValue = "", required = false) String startTime,
             @ApiParam(value = "通知结束时间") @RequestParam(value = "endTime", defaultValue = "", required = false) String endTime,
             @ApiParam(value = "客户Id") @RequestParam(value = "clientId", defaultValue = "", required = false) Long clientId,
             @ApiParam(value = "物料Id") @RequestParam(value = "materielId", defaultValue = "", required = false) Long materielId,
             @ApiParam(value = "部门Id") @RequestParam(value = "deptId", defaultValue = "", required = false) Long deptId,
             @ApiParam(value = "规格型号") @RequestParam(value = "specification", defaultValue = "", required = false) String specification,
             @ApiParam(value = "审核状态") @RequestParam(value = "auditSign", defaultValue = "", required = false) Long auditSign,
             @ApiParam(value = "销售员") @RequestParam(value = "salesPerson", defaultValue = "", required = false) Long salesPerson,
             @ApiParam(value = "制单人Id") @RequestParam(value = "createBy", defaultValue = "", required = false) Long createBy
             ) throws Exception {
         try {
             Map<String, Object> params = Maps.newHashMapWithExpectedSize(16);
             params.put("offset", (page - 1) * pageSize);
             params.put("limit", pageSize);
             params.put("deliveryCode",deliveryCode);
             params.put("clientName",clientName);
             params.put("materielName",materielName);
             params.put("startTime",startTime);
             params.put("endTime",endTime);
             params.put("clientId",clientId);
             params.put("materielId",materielId);
             params.put("deptId",deptId);
             params.put("specification",specification);
             params.put("auditSign",auditSign);
             params.put("salesPerson",salesPerson);
             params.put("createBy",createBy);
             return iDeliveryNoteService.findList(params);
         }catch (Exception e) {
             e.printStackTrace();
             LOGGER.error(e.getMessage(),e);
             throw e;
         }
     }

    @EvApiByToken(value = "/apis/deliveryNote/getDetail", method = RequestMethod.POST)
    @ApiOperation("获取发货通知详情")
    public R getDetail(
            @ApiParam(value = "发货通知Id", required = true) @RequestParam(value = "id", defaultValue = "") Long id) throws Exception {
        return iDeliveryNoteService.getDetail(id);
    }

    @EvApiByToken(value = "/apis/deliveryNote/findDialog", method = RequestMethod.POST, apiTitle = "发货通知列表查询")
    @ApiOperation("销售出库引用的发货通知单据")
    public R findDialog(
            @ApiParam(value = "当前第几页", required = true) @RequestParam(value = "page", defaultValue = "1") int page,
            @ApiParam(value = "一页多少条", required = true) @RequestParam(value = "pageSize", defaultValue = "20") int pageSize,
            @ApiParam(value = "通知开始时间") @RequestParam(value = "startTime", defaultValue = "", required = false) String startTime,
            @ApiParam(value = "通知结束时间") @RequestParam(value = "endTime", defaultValue = "", required = false) String endTime,
            @ApiParam(value = "发货通知明细Id") @RequestParam(value = "id", defaultValue = "", required = false) Long id,
            @ApiParam(value = "客户Id") @RequestParam(value = "clientId", defaultValue = "", required = false) Long clientId,
            @ApiParam(value = "审核状态") @RequestParam(value = "auditSign", defaultValue = "", required = false) Long auditSign,
            @ApiParam(value = "物料名称") @RequestParam(value = "materielName",defaultValue = "",required = false) String materielName
    ) throws Exception {
        try {
            Map<String, Object> params = Maps.newHashMapWithExpectedSize(16);
            params.put("page",page);
            params.put("pageSize",pageSize);
            params.put("clientId",clientId);
            params.put("startTime",startTime);
            params.put("id",id);
            params.put("endTime",endTime);
            params.put("auditSign",auditSign);
            params.put("materielName",materielName);
            return iDeliveryNoteService.findDialog(params);
        }catch (Exception e) {
            e.printStackTrace();
            LOGGER.error(e.getMessage(),e);
            throw e;
        }
    }

    @EvApiByToken(value = "/apis/deliveryNote/batchDelete", method = RequestMethod.POST)
    @ApiOperation("批量删除发货通知")
    public R batchDelete(
            @ApiParam(value = "发货通知Id列表", required = true) @RequestParam(value = "ids", defaultValue = "")List<Long> ids) throws Exception {
        if (CollectionUtils.isEmpty(ids)) {
            return R.error("未接收到发货通知主键！");
        }
        return iDeliveryNoteService.batchDelete(ids);
    }

    @ResponseBody
    @EvApiByToken(value = "/apis/deliveryNote/exportExcel", method = RequestMethod.GET, apiTitle = "导出发货通知")
    @ApiOperation("导出发货通知")
    public void exportExcel(
            @ApiParam(value = "通知单号") @RequestParam(value = "deliveryCode", defaultValue = "", required = false) String deliveryCode,
            @ApiParam(value = "客户名称") @RequestParam(value = "clientName", defaultValue = "", required = false) String clientName,
            @ApiParam(value = "物料名称") @RequestParam(value = "materielName", defaultValue = "", required = false) String materielName,
            @ApiParam(value = "通知开始时间") @RequestParam(value = "startTime", defaultValue = "", required = false) String startTime,
            @ApiParam(value = "通知结束时间") @RequestParam(value = "endTime", defaultValue = "", required = false) String endTime,
            @ApiParam(value = "客户Id") @RequestParam(value = "clientId", defaultValue = "", required = false) Long clientId,
            @ApiParam(value = "物料Id") @RequestParam(value = "materielId", defaultValue = "", required = false) Long materielId,
            @ApiParam(value = "部门Id") @RequestParam(value = "deptId", defaultValue = "", required = false) Long deptId,
            @ApiParam(value = "规格型号") @RequestParam(value = "specification", defaultValue = "", required = false) String specification,
            @ApiParam(value = "审核状态") @RequestParam(value = "auditSign", defaultValue = "", required = false) Long auditSign,
            @ApiParam(value = "销售员") @RequestParam(value = "salesPerson", defaultValue = "", required = false) Long salesPerson,
            @ApiParam(value = "制单人Id") @RequestParam(value = "createBy", defaultValue = "", required = false) Long createBy,
            HttpServletRequest request,
            HttpServletResponse response,
            ModelMap modelMap
    ) throws Exception {
        try {
            Map<String, Object> params = Maps.newHashMapWithExpectedSize(16);
            params.put("deliveryCode",deliveryCode);
            params.put("clientName",clientName);
            params.put("materielName",materielName);
            params.put("startTime",startTime);
            params.put("endTime",endTime);
            params.put("clientId",clientId);
            params.put("materielId",materielId);
            params.put("deptId",deptId);
            params.put("specification",specification);
            params.put("auditSign",auditSign);
            params.put("salesPerson",salesPerson);
            params.put("createBy",createBy);
            List<Map<String, Object>> list = iDeliveryNoteService.getDeliveryNoteList(params);
            JSONArray format = JSONUtils.format(list);
            String chineseName = "发货通知";
            String fileName = "poi/delivery_note.xlsx";
            EasyPoiUtils.processingExport(request,response,modelMap,fileName,format,chineseName);
        }catch (Exception e) {
            e.printStackTrace();
            LOGGER.error(e.getMessage(),e);
            throw e;
        }
    }

}
